package com.qf.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * @version v1.0.0
 * @Configuration: 类注解， 标记次注解的类相当于 配置文件
 * @Bean： 方法注解，可以把方法的返回值存储到容器中, 适合创建引入的第三方类对象
 * @PropertySource(value = "classpath:jdbc.properties"): 加载properties文件
 * @Value(): 给（简单类型）属性赋值
 * @ComponentScan(basePackages = "com.qf.service"): 包扫描
 * @Description:
 * @author: 赵英杰
 * @date: 2022.02.10
 * @since 1.0
 */
@Configuration
@PropertySource(value ="classpath:jdbc.properties")
public class SpringConfig {

    @Value("${jdbc.driverClass}")
    private String driverClass;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DruidDataSource createDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean createSqlSessionFactory(@Autowired DataSource dataSource){
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setTypeAliasesPackage("com.qf.pojo");
        return sessionFactoryBean;
    }

}
